
/*
 * Copyright (c) 2013.
 * 游戏服务器核心代码编写人陈磊拥有使用权
 * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.pveCoreController;

import com.dc.gameserver.proto.gameserverprotoVo.selectLevelRequest;
import com.dc.gameserver.proto.gameserverprotoVo.selectLevelResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TPointPrograss;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.gamemodel.LevelModel;
import com.dc.gameserver.serverCore.model.gamemodel.MonsterModel;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.battle.BaseBattle;
import com.dc.gameserver.serverCore.service.battle.PlayerMonsterBattle;
import com.dc.gameserver.serverCore.service.character.Monster;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 石头哥哥
 *         Project : LandlordsServer
 *         Date: 13-6-4
 *         Time: 上午11:49
 *         Connect: 13638363871@163.com
 *         玩家点击关卡 开始战斗，初始化怪物
 */
@Service
@SuppressWarnings("unchecked")
public class selectLevelController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;
    @Resource
    private PlayerService  playerService;

    /**
     */
    @Override
    public void PostConstruct() {
        int ID= selectLevelRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=selectLevelRequest.getDefaultInstance();
    }

     /**
     * messageLite数据结构体分发
     *
     * @param messageLite
     * @param player
     * @throws Exception
      *
      * 匹配玩家，初始化出牌数据
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {

        selectLevelRequest levelRequest= (selectLevelRequest) messageLite;
        int levelID=levelRequest.getLevelID();  // 关卡编号 level
        levelRequest=null;
        LevelModel levelModel=ScriptFactory.pointModelFastMap.get(levelID); //关卡模型   only read
        int roleID= player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
        //如果是神识关卡，检查神识上限是否足够
        if (levelModel.getLevelType()== 0x2){
            int roleMaxSoul=tRole.getRolemaxsual();
            if (roleMaxSoul<levelModel.getSoulNeed()){
                selectLevelResponse.Builder  infoBuilder=selectLevelResponse.newBuilder();
                infoBuilder.setLevelID(levelID);
                infoBuilder.setSelectLevelStatus(0x4);//
                player.DirectSend(wrappedBufferShort(selectLevelResponse.msgID.ID_VALUE,infoBuilder.build()));
                return;
            }
        }

        //1.auto recovery soul , time  消耗之前  自动回复
        boolean autoChange=player.autoRecoverySoul(tRole);
        if (autoChange){
            playerService.updateRole(tRole);
            modelCacheService.addObjectPrefixID(role_prefix,tRole);
            tRole= (TRole) modelCacheService.findByPrefixID(role_prefix); //retry get from cache
        }


        //2.检查玩家当前神识是否足够
        int soulCast=levelModel.getSoulCost();
        int roleSoul=tRole.getRolesual();//角色神识值
        if (soulCast>roleSoul){
            selectLevelResponse.Builder  infoBuilder=selectLevelResponse.newBuilder();
            infoBuilder.setLevelID(levelID);
            infoBuilder.setSelectLevelStatus(0x3);//操作失败  操作的次数超过上限
            player.DirectSend(wrappedBufferShort(selectLevelResponse.msgID.ID_VALUE,infoBuilder.build()));
            return;
        }


        //检查player 每天完成次数是否用完
        int completeMaxPerDay=levelModel.getCompleteMaxPerDay();
        String prograss_prefix=ModelCacheService.POINT_PROGRASS_PREFIX(roleID);
        List<TPointPrograss> prograssList= (List<TPointPrograss>) modelCacheService.findListByPrefixID(prograss_prefix);
        TPointPrograss  _prograss=null;
        for (TPointPrograss prograss:prograssList){
            if (prograss.getPointid()==levelID){
                _prograss=prograss;
                break;
            }
        }

        if (_prograss!=null&&_prograss.getPointpercompletenum()>=completeMaxPerDay){
            selectLevelResponse.Builder  infoBuilder=selectLevelResponse.newBuilder();
            infoBuilder.setLevelID(levelID);
            infoBuilder.setSelectLevelStatus(0x2);//操作失败  操作的次数超过上限
            player.DirectSend(wrappedBufferShort(selectLevelResponse.msgID.ID_VALUE,infoBuilder.build()));
            return;
        }

        //扣除玩家的神识值,此时神识值发生改变 ，计算时间戳
        tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
        int afterSoul=roleSoul-soulCast;  //扣除当前神识值
        tRole.setRolesual(afterSoul);
        player.sendSoulTime(tRole,0);  //flush soul

        //update and cache
        playerService.updateRole(tRole);
        modelCacheService.addObjectPrefixID(role_prefix,tRole);

        MonsterModel monsterModel=ScriptFactory.monsterModelFastMap.get(levelModel.getEnemyID());// only read
        // 初始化玩家战斗属性  ，怪物战斗属性
        BaseBattle playerNpcBattle=
                new PlayerMonsterBattle(player,new Monster(monsterModel,levelModel),levelModel);
        //开始战斗
        playerNpcBattle.run();
    }
}
